package com.songshuai.webapi.permission.service.impl;

import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.permission.dao.YxDepartmentLeaderMapper;
import com.songshuai.webapi.permission.dao.YxDepartmentRelationMapper;
import com.songshuai.webapi.permission.dao.YxUserRolesMapper;
import com.songshuai.webapi.permission.dto.po.QuickPositioningUserReqPojo;
import com.songshuai.webapi.permission.dto.request.QuickPositioningUserReqDto;
import com.songshuai.webapi.permission.dto.response.QuickPositioningUserRespDto;
import com.songshuai.webapi.permission.service.CurrentUserService;
import com.songshuai.webapi.permission.service.RoleUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Slf4j
@Service
public class RoleUserServiceImpl implements RoleUserService {


    @Autowired
    private CurrentUserService currentUserService;


    @Autowired
    private YxDepartmentLeaderMapper departmentLeaderMapper;

    @Autowired
    private YxDepartmentRelationMapper departmentRelationMapper;


    @Autowired
    private YxUserRolesMapper userRolesMapper;

    /**
     * 查询该角色关联的用户列表
     *
     * @param puickPositioningUserReqDto
     * @return
     */
    @Override
    public QueryResponse quickPositioningUser(QuickPositioningUserReqDto puickPositioningUserReqDto) {
        Integer currentLoginId = currentUserService.getUserId();
        QuickPositioningUserReqPojo quickPositioningUserReqPojo = new QuickPositioningUserReqPojo();
        PropertiesUtil.copyProperties(puickPositioningUserReqDto, quickPositioningUserReqPojo);
        if(null==quickPositioningUserReqPojo.getMaxSize()){
            quickPositioningUserReqPojo.setMaxSize(50);
        }
        //设置登录用户ID
        quickPositioningUserReqPojo.setLoginUserId(currentLoginId);
        boolean isSysAdmin=currentUserService.CheckIsSysAdmin();
        log.info("当前登录人是否是超级管理员："+isSysAdmin+"，人员ID是"+currentLoginId);
        //如果是超级管理员，则查询全部
        if(!isSysAdmin){
            //如果不是超级管理员，则校验登录用户是不是部门领导
            //quickPositioningUserReqPojo.setSysAdmin(isSysAdmin);
            if (departmentLeaderMapper.countByUserId(currentLoginId) > 0) {
                //校验登录用户是不是部门领导
                //如果是部门领导，可查询子级部门以及其用户
                Set<Integer> deptIdList = departmentRelationMapper.selectSubDeptIdList(currentLoginId);
                quickPositioningUserReqPojo.setDeptIdList(deptIdList);
            }
        }

        List<QuickPositioningUserRespDto> QuickPositioningUserRespList = userRolesMapper.queryUserNameInfos(quickPositioningUserReqPojo);
        String connectStr="-";

        List<Map> resultList = new ArrayList<>();
        for (QuickPositioningUserRespDto item: QuickPositioningUserRespList ) {
            if(!StringUtils.isEmpty(item.getCellphone())){
                if(!StringUtils.isEmpty(item.getDeptName())){
                    item.setNameInfo(item.getTrueUserName()+connectStr+item.getDeptName()+connectStr+item.getCellphone());
                }else{
                    item.setNameInfo(item.getTrueUserName()+connectStr+item.getCellphone());
                }
            }else {
                if(!StringUtils.isEmpty(item.getDeptName())){
                    item.setNameInfo(item.getTrueUserName()+connectStr+item.getDeptName()+connectStr+item.getEmail());
                }else{
                    item.setNameInfo(item.getTrueUserName()+connectStr+item.getEmail());
                }
               //item.setNameInfo(item.getTrueUserName()+connectStr+item.getEmail()+connectStr+item.getDeptName());
            }
            Map map = new HashMap();
            map.put("id",item.getUserId());
            map.put("name",item.getNameInfo());
            map.put("typeId",item.getUserId());
            map.put("parentId",0);
            map.put("isCheck",false);
            map.put("leaf",true);
            resultList.add(map);
        }

        return QueryResponse.setResData(resultList, ResultStatus.SUCCESS);
    }



}

